#include <boost/filesystem/path.hpp>
#include <boost/foreach.hpp>
#include <boost/ptr_container/ptr_vector.hpp>
#include "word_tree.h++"
#include "tfidf_calculator.h++"
#include "file_reader.h++"

class Serializer;

class Indexer {
	Serializer& serializer;

	std::auto_ptr<WordTree::UnserializedNode> root_node;
	boost::ptr_vector<UnserializedDocument> documents;
	TfIdfCalculator<UnserializedDocument> calculator;
	typedef WordTree::UnserializedNode::UnserializedMatchingDocument mdoc;
	std::set<mdoc*> postings_to_serialize;

	boost::filesystem::path current_basepath;

	void index_file(const boost::filesystem::path& file);

	void calculate_tfidfs(WordTree::UnserializedNode& node);
	public:
	// if i could make it public only to that calculator ...
	void found_word(UnserializedDocument& doc, const Word& word);
	void ignoring_word(const Word&) {}
	public:
	void init();
	Indexer(Serializer& serializer);
	void index_dir(const boost::filesystem::path& path);
	// might be uneeded, maybe there is already an implicit
	// convertion of std::string to boost::filesystem::path.
	// yes there is.
	// void index_dir(const std::string& str) {
	//	index_dir(boost::filesystem::path(str));
	// }
	void finish_and_serialize();
};
